/**
 * Project Name: mms-provider-user
 * File Name: AccountServiceImpl
 * Package Name: com.scb.dev.mms.service.impl
 * Date: 2018/11/27 2:52 PM
 * Copyright (c) 2018, Wang, Haoyue All Rights Reserved.
 */
package com.scb.dev.mms.service.impl;

import com.scb.dev.mms.common.ResponseResult;
import com.scb.dev.mms.common.ResultType;
import com.scb.dev.mms.common.StatusCode;
import com.scb.dev.mms.dao.AccountMapper;
import com.scb.dev.mms.dao.EmployeeMapper;
import com.scb.dev.mms.dto.AccountFormDTO;
import com.scb.dev.mms.feign.AuthorityFeign;
import com.scb.dev.mms.pojo.Account;
import com.scb.dev.mms.pojo.Employee;
import com.scb.dev.mms.service.IAccountService;
import com.scb.dev.mms.service.IEmployeeService;
import com.scb.dev.mms.util.UUIDTool;
import com.scb.dev.mms.vo.AccountInfoVO;
import com.scb.dev.mms.vo.LoginVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: AccountServiceImpl
 * Description: AccountService
 * Date: 2018/11/27 2:52 PM
 *
 * @author Wang, Haoyue
 * @version V1.0
 * @since JDK 1.8
 */
@Slf4j
@Service
public class AccountServiceImpl implements IAccountService {

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private EmployeeMapper employeeMapper;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private AuthorityFeign menuFeign;

    /**
     * insertAccount: add account
     *
     * @param account  new account
     * @return  success: {true, "添加成功", 202}
     */
    @Override
    public ResponseResult insertAccount(Account account, String accountId) {
        if (account == null) {
            log.info("参数为空,添加账户失败");
            return new ResponseResult(false, ResultType.PARAMETER_NULL, StatusCode.PARAMETERNULL);
        }
        account.setAccountId(UUIDTool.generateUUID());
        account.setAccountCreatorAccountId(accountId);
        account.setAccountPassword("123456");
        if (accountMapper.insertSelective(account) == 0) {
            log.info("添加账户执行失败");
            return new ResponseResult(false, ResultType.ADD_ERROR, StatusCode.ADDERROR);
        }
        log.info("Account Service -> insertAccount 添加账户成功");
        return new ResponseResult(true, ResultType.ADD_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }

    /**
     * deleteAccount: delete account by accountId
     *
     * @param accountId    account id
     * @return  success: {true, "删除成功", 203}
     */
    @Override
    public ResponseResult deleteAccount(String accountId, String curr_accountId) {
        if (StringUtils.isBlank(accountId)) {
            log.info("参数为空,删除账户失败");
            return new ResponseResult(false, ResultType.PARAMETER_NULL, StatusCode.PARAMETERNULL);
        }
        int result = accountMapper.deleteByPrimaryKey(accountId);
        if (result == 0) {
            log.info("删除账户执行失败");
            return new ResponseResult(false, ResultType.DELETE_ERROR, StatusCode.DELETEERROR);
        }
        log.info("删除了 " + result + " 条账户数据");
        Account account = new Account();
        account.setAccountId(accountId);
        account.setAccountUpdatorAccountId(curr_accountId);
        accountMapper.updateByPrimaryKeySelective(account);
        log.info("Account Service -> deleteAccount 删除账户成功");
        return new ResponseResult(result, true, ResultType.DELETE_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }

    /**
     * updateAccount: update account
     *
     * @param account  new account
     * @return  success: {true, "更新成功", 204}
     */
    @Override
    public ResponseResult updateAccount(Account account, String accountId) {
        if (account == null) {
            log.info("参数为空,更新账户信息失败");
            return new ResponseResult(false, ResultType.PARAMETER_NULL, StatusCode.PARAMETERNULL);
        }
        account.setAccountUpdatorAccountId(accountId);
        if (accountMapper.updateByPrimaryKeySelective(account) == 0) {
            log.info("更新账户信息执行失败");
            return new ResponseResult(false, ResultType.UPDATE_ERROR, StatusCode.UPDATEERROR);
        }
        log.info("Account Service -> updateAccount 更新账户成功");
        return new ResponseResult(true, ResultType.UPDATE_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }

    /**
     * queryOne: query one account by accountId
     *
     * @param accountId    account id
     * @return  success: {true, "查询成功", 201}
     */
    @Override
    public ResponseResult queryOne(String accountId) {
        if (StringUtils.isBlank(accountId)) {
            log.info("参数为空,查询账户信息失败");
            return new ResponseResult(false, ResultType.PARAMETER_NULL, StatusCode.PARAMETERNULL);
        }
        AccountInfoVO accountInfoVO = new AccountInfoVO();
        Account account = accountMapper.selectByPrimaryKey(accountId);
        if (account == null) {
            log.info("查询账户信息执行失败");
            return new ResponseResult(false, ResultType.FIND_ERROR, StatusCode.FINDERROR);
        } else {
            Employee employee = employeeMapper.selectByPrimaryKey(account.getAccountEmployeeId());
            log.info("账户查询结果: " + account.toString());
            accountInfoVO.setAccountId(account.getAccountId());
            accountInfoVO.setAccountName(account.getAccountName());
            // 通过职员编号查出职员姓名
            accountInfoVO.setAccountEmployeeName(employeeMapper.selectEmployeeNameByPrimaryKey(account.getAccountEmployeeId()));
            // 通过职员编号查出职员昵称
            accountInfoVO.setAccountEmployeeNickname(employeeMapper.selectEmployeeNicknameByPrimaryKey(account.getAccountEmployeeId()));
            accountInfoVO.setAccountCreatorAccountName(accountMapper.selectAccountNameByPrimaryKey(account.getAccountCreatorAccountId()));
            accountInfoVO.setAccountCreatedTime(account.getAccountCreatedTime());
            if (account.getAccountUpdatorAccountId() != null) {
                accountInfoVO.setAccountUpdatorAccountName(accountMapper.selectAccountNameByPrimaryKey(account.getAccountUpdatorAccountId()));
            }
            accountInfoVO.setAccountUpdatedTime(account.getAccountUpdatedTime());
            accountInfoVO.setAccountRemarks(account.getAccountRemarks());
            log.info("账户信息VO: " + accountInfoVO.toString());
        }
        log.info("Account Service -> queryOne 查询账户成功");
        return new ResponseResult(accountInfoVO, true, ResultType.FIND_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }

     /**
     * queryName: query accountName by accountId
     *
     * @param accountId    account id
     * @return  success: {true, "查询成功", 201}
     */
    @Override
    public ResponseResult queryName(String accountId) {
        if (StringUtils.isBlank(accountId)) {
            log.info("参数为空,查询账户名称失败");
            return new ResponseResult(false, ResultType.PARAMETER_NULL, StatusCode.PARAMETERNULL);
        }
        String accountName = accountMapper.selectAccountNameByPrimaryKey(accountId);
        if (accountName == null) {
            log.info("查询账户名称执行失败");
            return new ResponseResult(false, ResultType.FIND_ERROR, StatusCode.FINDERROR);
        }
        log.info("Account Service -> queryName 查询账户名称成功");
        return new ResponseResult(accountName, true, ResultType.FIND_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }

    /**
     * queryAll: query all accounts
     *
     * @return  success: {accountList, true, "查询成功", 201}
     */
    @Override
    public ResponseResult queryAll() {
        List<Account> accountList = accountMapper.findAllAccount();
        if (CollectionUtils.isEmpty(accountList)) {
            log.info("没有查到账户");
            return new ResponseResult(false, ResultType.FIND_NULL, StatusCode.FINDNULL);
        }
        List<AccountInfoVO> accountInfoVOList = new ArrayList<>();
        for (Account account : accountList) {
            AccountInfoVO accountInfoVO = new AccountInfoVO();
            accountInfoVO.setAccountId(account.getAccountId());
            accountInfoVO.setAccountName(account.getAccountName());
            // 通过职员编号查出职员姓名
            accountInfoVO.setAccountEmployeeName(employeeMapper.selectEmployeeNameByPrimaryKey(account.getAccountEmployeeId()));
            // 通过职员编号查出职员昵称
            accountInfoVO.setAccountEmployeeNickname(employeeMapper.selectEmployeeNicknameByPrimaryKey(account.getAccountEmployeeId()));
            accountInfoVO.setAccountCreatorAccountName(accountMapper.selectAccountNameByPrimaryKey(account.getAccountCreatorAccountId()));
            accountInfoVO.setAccountCreatedTime(account.getAccountCreatedTime());
            if (account.getAccountUpdatorAccountId() != null) {
                accountInfoVO.setAccountUpdatorAccountName(accountMapper.selectAccountNameByPrimaryKey(account.getAccountUpdatorAccountId()));
            }
            accountInfoVO.setAccountUpdatedTime(account.getAccountUpdatedTime());
            accountInfoVO.setAccountRemarks(account.getAccountRemarks());
            accountInfoVOList.add(accountInfoVO);
        }
        log.info("Account Service -> queryAll 查询所有账户成功");
        return new ResponseResult(accountInfoVOList, true, ResultType.FIND_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }

    /**
     * accountLogin: account Login
     *
     * @param accountFormDTO
     * @return
     */
    @Override
    public ResponseResult accountLogin(AccountFormDTO accountFormDTO) {
        if (accountFormDTO == null) {
            // 登陆表单为空
            return new ResponseResult(false, ResultType.PARAMETER_NULL, StatusCode.PARAMETERNULL);
        }
        if (accountFormDTO.getAccountName() == null || "".equals(accountFormDTO.getAccountName())) {
            // 帐户名为空
            return new ResponseResult(false, ResultType.ACCOUNT_NAME_NULL, StatusCode.ACCOUNTNAMENULL);
        }
        // 通过帐户名查询账户所有信息
        Account account = accountMapper.selectByAccountName(accountFormDTO.getAccountName());
        if (account == null) {
            // 账户不存在
            return new ResponseResult(false, ResultType.ACCOUNT_NOT_EXIST, StatusCode.ACCOUNTNOTEXIST);
        }
        if (!accountFormDTO.getAccountPassword().equals(account.getAccountPassword())) {
            // 密码错误
            return new ResponseResult(false, ResultType.PASSWORD_ERROR, StatusCode.PASSWORDERROR);
        }
        // 密码正确，通过职员ID查到职员昵称
        ResponseResult result = employeeService.queryNickname(account.getAccountEmployeeId());
        if (result.getCode().intValue() != 201) {
            // 查询失败
            return new ResponseResult(false, result.getMessage(), result.getCode());
        }
        // 查询成功，构造LoginVO
        LoginVO loginVO = new LoginVO();
        loginVO.setEmployeeId(account.getAccountEmployeeId());
        loginVO.setAccountId(account.getAccountId());
        loginVO.setEmployeeNickname((String) result.getData());

        // ping远程接口获取menuTree
        Employee employee = employeeMapper.selectByPrimaryKey(account.getAccountEmployeeId());
        if (employee == null) {
            // 查询失败
            return new ResponseResult(false, ResultType.FIND_ERROR, StatusCode.FINDERROR);
        }
        ResponseResult remoteResult = menuFeign.getMenuTree(employee.getEmployeeRoleId());
        loginVO.setMenuTree(remoteResult.getData());
        log.info("Account Service -> accountLogin 登录成功");
        return new ResponseResult(loginVO, true, ResultType.LOGIN_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }
}
